/* eslint-disable */

const getDefaultState = () => {
    return {
        leftNavData: [],
        selectedData: [],
        activeItem: {}
        // tabNavData: [],
    }
};

const state = getDefaultState();

const mutations = {
    findParents(state, payload) {
        if (payload.item.meta.hasChildren) {
            payload.item.meta.isExpanded = !payload.item.meta.isExpanded
        } else {
            if (!payload.item.meta.isSelected) {
                if (state.selectedData.length > 0) {
                    state.selectedData.forEach(function (item) {
                        item.meta.isSelected = false;
                    })
                }
                payload.item.meta.isSelected = true;
                if (JSON.stringify(state.activeItem) !== '{}') {
                    state.activeItem.meta.isSelected = false
                }
                state.activeItem = {};
                state.activeItem = payload.item;
                if (!hasSelectedLeftNavData(state.selectedData, payload.item)) {
                    state.selectedData.push(payload.item)
                }
            }
        }
    },
    init(state, payload) {
        state.leftNavData = payload.data
        // console.log(state.data)
    },
    resetState(state) {
        Object.assign(state, getDefaultState())
    },
    setLeftState(state, data) {

    },
    closeSelNavTab(state, payload) {
        payload.item.meta.isSelected = false;
        let index = state.selectedData.findIndex(item => item.name === payload.item.name);
        state.selectedData.splice(index, 1);
        if (payload.item.name === state.activeItem.name) {
            let tab = state.selectedData[index] === undefined ? state.selectedData[index - 1] : state.selectedData[index]
            if (tab !== undefined) {
                tab.meta.isSelected = true;
                state.activeItem = {};
                state.activeItem = tab;
                payload.app.$router.push({path: tab.path})
            }
        }
    },
};

const getters = {
    selectedTabNav: state => {
        let sel = [];
        state.selectedData.forEach(function (item) {
            state.leftNavData.forEach(function (item1) {
                if (item1.children !== undefined) {
                    item1.children.forEach(function (item2) {
                        if (item.name === item2.name) {
                            sel.push(item2)
                        }
                    })
                }
            })
        });
        state.selectedData = [];
        return state.selectedData = sel
    },
    selectedLeftNav: state => {
        if (JSON.stringify(state.activeItem !== '{}')) {
            for (let i = 0; i < state.leftNavData.length; i++) {
                if (state.leftNavData[i].children !== undefined) {
                    state.leftNavData[i].children.forEach(function (item) {
                        if (item.name === state.activeItem.name) {
                            item.meta.isSelected = true;
                            state.activeItem = {};
                            state.activeItem = item;
                            return state.leftNavData;
                        }
                    })
                }
            }
        }
        return state.leftNavData;
    },
    leftNavDataLen: state => {
        return state.leftNavData.length;
    },

};

function hasSelectedLeftNavData(data, sel) {
    for (let i = 0; i < data.length; i++) {
        if (data[i].name === sel.name) return true;
    }
    return false;
}

export default {
    state,
    mutations,
    getters
};